//---------------------------------------------------------------------------


#pragma hdrstop

#include "TBookMarks.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)
TBookMarks::TBookMarks(Client *client)
{
	if(NULL==client)return;
	_client=client;
	bookmarks=new BookmarkStorage(_client);
	bookmarks->registerBookmarkHandler( this );
}
//---------------------------------------------------------------------------
TBookMarks::~TBookMarks()
{
   if(bookmarks){
		bookmarks->removeBookmarkHandler();
		delete bookmarks;
   }
}
//---------------------------------------------------------------------------
void TBookMarks::handleBookmarks( const BookmarkList &bList, const ConferenceList &cList )
{
	  BookList=bList;
	  ConfList=cList;
	  BookmarkList::const_iterator it_b = bList.begin();
	  for( ; it_b != bList.end(); it_b++ )
	  {
		if(FOnBookMarkItem)FOnBookMarkItem(Utf8ToAnsi((*it_b).name.c_str()).t_str(), (*it_b).url.c_str() );
	  }

	  ConferenceList::const_iterator it_c = cList.begin();
	  for( ; it_c != cList.end(); it_c++ )
	  {
		if(FOnConfrenceItem)FOnConfrenceItem((*it_c).jid.c_str(), Utf8ToAnsi((*it_c).name.c_str()).t_str(),
				Utf8ToAnsi((*it_c).nick.c_str()).t_str(), (*it_c).password.c_str(),(*it_c).autojoin);
	  }
}
//---------------------------------------------------------------------------
void __fastcall TBookMarks::GetBookMarksAndConferences()
{
	 if(NULL==_client)return;
	 if(_client->state()!=StateConnected)return;
	 bookmarks->requestBookmarks();
}
//---------------------------------------------------------------------------
BookmarkList::iterator __fastcall TBookMarks::DeleteBookMark(int index) //-1 al
{
	 if(index>=BookList.size())return BookList.begin();
	  if(index==-1)
	  {	BookList.clear();
			return BookList.begin();
	  }

		  BookmarkList::iterator it = BookList.begin();
		  for(int i=0;i<index&&it!=BookList.end();i++,it++);
		  BookList.erase(it--);
		  if(BookList.empty())
			return BookList.begin();
		  else
			return it++;
}
//---------------------------------------------------------------------------
void __fastcall TBookMarks::SaveBookMark()
{
	  bookmarks->storeBookmarks( BookList, ConfList );
}
//---------------------------------------------------------------------------
BookmarkList::iterator __fastcall TBookMarks::EditBookMark(unsigned int index,std::string url,std::string name)
{
	if(index>=BookList.size()||BookList.empty())return  BookList.begin();
	 BookmarkList::iterator it =BookList.begin();
	 for(int i=0;i<index&&it!=BookList.end();i++,it++);
	 BookList.erase(it--);
	 it++;
	 BookmarkListItem bItem;
	 bItem.name=AnsiToUtf8(name.c_str()).c_str();
	 bItem.url=url;
	 BookList.insert(it, bItem);
	 it--;
	 return it;
}
//---------------------------------------------------------------------------
BookmarkList::iterator __fastcall TBookMarks::AddBookMark(std::string url,std::string name)
{
	 if(url==EmptyString||name==EmptyString)return  BookList.begin();
	  BookmarkListItem bItem;
	  bItem.name=AnsiToUtf8(name.c_str()).c_str();
	  bItem.url=url;
	  BookList.push_back( bItem );
	  return BookList.end();
}
//---------------------------------------------------------------------------
ConferenceList::iterator __fastcall TBookMarks::DeleteConference(int index) //-1 al
{
	 if(index>=ConfList.size())return ConfList.begin();
	  if(index==-1)
	  {	ConfList.clear();
			return ConfList.begin();
	  }

		  ConferenceList::iterator it = ConfList.begin();
		  for(int i=0;i<index&&it!=ConfList.end();i++,it++);
		  ConfList.erase(it--);
		  if(ConfList.empty())
			return ConfList.begin();
		  else
			return it++;
}
//---------------------------------------------------------------------------
void __fastcall TBookMarks::SaveConference()
{
	 bookmarks->storeBookmarks( BookList, ConfList );
}
//---------------------------------------------------------------------------
ConferenceList::iterator __fastcall TBookMarks::EditConference(unsigned int index,std::string jid,std::string name,std::string nick,std::string password,bool autojoin)
{
	if(index>=ConfList.size()||ConfList.empty())return  ConfList.begin();
	 ConferenceList::iterator it =ConfList.begin();
	 for(int i=0;i<index&&it!=ConfList.end();i++,it++);
	 ConfList.erase(it--);
	 it++;
	 ConferenceListItem cItem;
	 cItem.jid = jid;
	 cItem.name = AnsiToUtf8(name.c_str()).c_str();
	 cItem.nick = AnsiToUtf8(nick.c_str()).c_str();;
	 cItem.password = password;
	 cItem.autojoin = autojoin;
	 ConfList.insert(it, cItem);
	 it--;
	 return it;
}
//---------------------------------------------------------------------------
ConferenceList::iterator __fastcall TBookMarks::AddConference(std::string jid,std::string name,std::string nick,std::string password,bool autojoin)
{
	if(name==EmptyString||jid==EmptyString)return  ConfList.begin();
	  ConferenceListItem cItem;
	  cItem.jid = jid;
	  cItem.name = AnsiToUtf8(name.c_str()).c_str();
	  cItem.nick = AnsiToUtf8(name.c_str()).c_str();
	  cItem.password = password;
	  cItem.autojoin = autojoin;
	  ConfList.push_back( cItem );
	  return ConfList.end();
}
//---------------------------------------------------------------------------
